package leetcode;

import java.util.*;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-06 16:13
 * @ Description :
 */
public class BracketMatching
{
    public static void main(String[] args)
    {
        String s = "()][{}";
        System.out.println(isValid(s));
    }

    public static boolean isValid(String s)
    {
        if (s.length() % 2 != 0)
        {
            return false;
        }

        int leftIndex = 0;
        int rightIndex = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (isRight(s.charAt(i) + ""))
            {
                rightIndex = i;
            }
        }
        leftIndex = rightIndex - 1;
        while (rightIndex < s.length() && leftIndex >= 0)
        {
            //判断是否是一对
            if (!isCouple(s.charAt(leftIndex--) + "",s.charAt(rightIndex++) + ""))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean isCouple(String left,String right)
    {
        Map<String,String> temp = new HashMap<>();
        temp.put("(",")");
        temp.put("{","}");
        temp.put("[","]");

        return temp.get(left).equals(right);
    }

    public static boolean isRight(String s)
    {
        return s.equals(")") || s.equals("}") || s.equals("]");
    }


}
